package com.xca.znb.view;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.xca.znb.R;
import com.xca.znb.contact.UserAuthView;
import com.xca.znb.model.impl.UserAuthModelImpl;
import com.xca.znb.presenter.UserAuthPresenter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LoginActivity extends AppCompatActivity implements UserAuthView {
    private EditText etPhone, etPassword;
    private Button btnLogin;
    private UserAuthPresenter userAuthPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        etPhone = findViewById(R.id.login_input_phone);
        etPassword = findViewById(R.id.login_input_password);
        btnLogin = findViewById(R.id.login_loginBtn);

        UserAuthModelImpl userAuthModel = new UserAuthModelImpl(this);
        userAuthPresenter = new UserAuthPresenter(userAuthModel, this);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = etPhone.getText().toString();
                String password = etPassword.getText().toString();

                if (phone.length() != 11){
                    Toast.makeText(LoginActivity.this, "手机号码格式错误", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!phone.matches("[1]{1}\\d{10}")){
                    Toast.makeText(LoginActivity.this, "手机号码格式错误", Toast.LENGTH_SHORT).show();
                    return;
                }
                //将登录账号的电话进行共享
                SharedPreferences sharedPreferences = getSharedPreferences("loginPhone" , Context.MODE_PRIVATE) ;
                SharedPreferences.Editor editor = sharedPreferences.edit() ;
                editor.putString("phone" , phone);
                editor.apply();

                //获取当前时间：精确到秒
                LocalDateTime nowTime = LocalDateTime.now();
                String secTime = nowTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                String dayTime = nowTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

                double num = 0 ;
                String type = "充值" ;

                boolean isUserExists = userAuthPresenter.isUserExists(phone);
                if (isUserExists){
                    boolean isValidPassword = userAuthPresenter.isValidPassword(phone,password);
                    if (isValidPassword){
                        Intent intent = new Intent(LoginActivity.this, Main.class);
                        startActivity(intent);
                        finish();
                    }else {
                        Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
                    }
                }else {
                    new AlertDialog.Builder(LoginActivity.this)
                            .setMessage("用户不存在，是否注册？")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    userAuthPresenter.sign(phone,password);
                                    boolean isAdd = userAuthPresenter.isUserExists(phone);
                                    if (isAdd){
                                        Toast.makeText(LoginActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                        Intent intent = new Intent(LoginActivity.this, Main.class);
                                        startActivity(intent);
                                        finish();
                                    }else {
                                        Toast.makeText(LoginActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            })
                            .setNegativeButton("取消" , null)
                            .create().show();
                }
            }
        });
    }

    @Override
    public void showLoading() {
        // 显示加载指示器，例如ProgressBar
    }

    @Override
    public void hideLoading() {
    }

    @Override
    public void navigateToHome() {
        // 导航到主界面，例如HomeActivity
        Intent intent = new Intent(LoginActivity.this, Main.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void showError(String message) {
        // 显示错误信息，例如使用Toast或Snackbar
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}